<?php
namespace api\modules\shop\controllers;

use common\models\Admin;
use common\models\Banner;
use common\models\SupplierGoods;
use common\service\ConstantService;
use common\service\GlobalUrlService;
use common\service\SupplierGoodsService;

class IndexController extends ShopBaseController
{

    public function actionIndex(){
        echo '首页B端111';exit;
    }

    /**
     * 联系电话
     * @return string
     */
    public function actionGetPhone(){
        $phone = Admin::find()->select('phone')->where(['id'=>1])->asArray()->one();
        return $this->renderAjax($phone);
    }
    /**
     * 获取banner图
     */
    public function actionGetBanner(){
        if($this->isGet()){
            $lists = Banner::find()->where(['is_del'=>0,'show_platform'=>1])->asArray()->all();
            if(!empty($lists)){
                foreach ($lists as &$list){
                    $list['img_url'] = GlobalUrlService::buildCdnUrl($list['img_url']);
                }
            }

            return $this->renderJson($lists,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 获取商品列表
     */
    public function actionGetGoods(){
        if($this->isGet()){
            $goods_type = $this->get('goods_type');
            $p = intval( $this->get('page',1));
            $offset = ($p - 1) * $this->page_size;
            $goods = SupplierGoods::find()
                ->select(['id','goods_name','list_img','goods_type','display_sales'])
                ->where(['status'=>1,'is_del'=>0,'goods_type'=>$goods_type])
                ->offset( $offset )
                ->limit( $this->page_size )
                ->asArray()
                ->all();
            //这个地方需要获取属性里面的原价最高价和折扣价最低价
            $goods = SupplierGoodsService::GetAttrListPrice($goods);
            return $this->renderJson($goods,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }
}